package com.example.administrator.myapplication.evaluate;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.BmobApplication;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.tools.Advice;
import com.example.administrator.myapplication.tools.History;
import com.example.administrator.myapplication.tools.TestData;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SQLQueryListener;

/**
 * Created by Administrator on 2018/1/19.
 */

public class EvaluateMainActivity extends Activity{
    private Toolbar toolbar;
    private BmobApplication Apps;
    private ImageView iv_heard_assess;
    private ImageView iv_muscle_assess;
    private ImageView iv_soft_assess;
    private ImageView iv_body_assess;

    private TextView tv_heard_assess;
    private TextView tv_muscle_assess;
    private TextView tv_soft_assess;
    private TextView tv_body_assess;
    private TextView tv_total;
    private TextView tv_main_advice;

    //============================

    private String heart_score;
    private String muscle_score;
    private String soft_score;
    private String body_score;
    private String total_score;
    private String ad;

    private History hs;
    private

    //子线程刷新ui
    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                tv_main_advice.setText(ad);
                tv_main_advice.setMovementMethod(new ScrollingMovementMethod());
            }
        }
    }




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate_main);
        toolbar= (Toolbar) findViewById(R.id.toolbar_evaluate_main);
        toolbar.setNavigationIcon(R.mipmap.custom_back);
        toolbar.setTitle("综合评测");
        toolbar.setTitleMarginStart(200);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        init();
        setAttribute();

        new MyThread().start();


        iv_heard_assess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                skip(1);
            }
        });

        iv_muscle_assess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                skip(2);
            }
        });

        iv_soft_assess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                skip(3);
            }
        });

        iv_body_assess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                skip(4);
            }
        });



    }




    public void init(){
        iv_heard_assess = (ImageView) findViewById(R.id.iv_heard_logo);
        iv_muscle_assess = (ImageView) findViewById(R.id.iv_muscle_logo);
        iv_soft_assess = (ImageView) findViewById(R.id.iv_flexibility_logo);
        iv_body_assess = (ImageView) findViewById(R.id.iv_body_logo);

        tv_heard_assess= (TextView) findViewById(R.id.tv_heard_logo);
        tv_muscle_assess= (TextView) findViewById(R.id.tv_muscle_logo);
        tv_soft_assess= (TextView) findViewById(R.id.tv_flexibility_logo);
        tv_body_assess= (TextView) findViewById(R.id.tv_body_logo);
        tv_total= (TextView) findViewById(R.id.tv_total_score);
        tv_main_advice= (TextView) findViewById(R.id.tv_main_advice);


        Apps= (BmobApplication) getApplication();
    }

    public void   query(){
        BmobQuery<Advice> query = new BmobQuery<Advice>();
        String sql=null;
        if(Integer.parseInt(total_score)<60){
            sql="riB13223";
        }else{
            sql="qxsWM22M";
        }
        query.getObject(sql, new QueryListener<Advice>() {

            @Override
            public void done(Advice advice, BmobException e) {
                if(e==null){
                    ad=advice.getAdvice();
                    new MyHandler().sendEmptyMessage(1);
                }else{
                    System.out.println(e.getMessage());
                }
            }
        });



    }

    class MyThread extends Thread{
        public void run(){
            query();
        }
    }


    public void skip(int number){
        Apps.setFlag(number);
        Apps.setIntentflag(1);
        Intent intent =new Intent(this,TestAssess.class);
        Bundle bundle =new Bundle();
        bundle.putString("heart",heart_score);
        bundle.putString("muscle",muscle_score);
        bundle.putString("soft",soft_score);
        bundle.putString("body",body_score);
        intent.putExtras(bundle);
        startActivity(intent);

    }

    public void setAttribute(){
        Intent intent =getIntent();
        int history =intent.getIntExtra("historykey",3);
        //是从历史页面跳转过来的
        if(history<3){
            hs =Apps.list.get(history);
           heart_score=hs.getHeart();
           muscle_score=hs.getMuscle();
           soft_score=hs.getSoft();
           body_score=hs.getBody();
           total_score=hs.getTotal();
        }else if(history==3){
            //从主页面跳转过来的
            hs=Apps.now_history;
            heart_score=hs.getHeart();
            muscle_score=hs.getMuscle();
            soft_score=hs.getSoft();
            body_score=hs.getBody();
            total_score=hs.getTotal();
        }
        tv_heard_assess.setText(heart_score);
        tv_muscle_assess.setText(muscle_score);
        tv_soft_assess.setText(soft_score);
        tv_body_assess.setText(body_score);
        tv_total.setText(total_score);
    }

}
